package org.assistant.common.web.servlet.mvc;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.assistant.common.exception.ParseMethodException;

/**
 * 从URL地址中获取请求的Controller中的函数。<br>
 * 比如：URL地址为/hcm/login.t?m=index，则getMethodName()函数得到的函数为：index。
 * 
 * @author zhangQ
 * 
 */
public class ParameterMethodNameParser implements MethodNameParser {
	protected final static Log log = LogFactory
		.getLog(ParameterMethodNameParser.class);
	protected final static boolean isDebugEnabled = log.isDebugEnabled();

	/**
	 * 获取当前请求的方法名
	 */
	public String getMethodName(HttpServletRequest request)
			throws ParseMethodException {
		return getHandlerMethodName(request);
	}

	public String getHandlerMethodName(HttpServletRequest request)
			throws ParseMethodException {
		if (isDebugEnabled) {
			log.debug("request URL:" + request.getRequestURL());
			log.debug("request parameter:" + request.getQueryString());
		}
		String methodName = null;
		if (request.getParameter("m") != null) {
			methodName = request.getParameter("m");
			if (isDebugEnabled) {
				log.debug("methodName1:" + methodName);
			}
		} else {
			throw new ParseMethodException(request.getRequestURI(), request
				.getMethod());
		}
		if (isDebugEnabled) {
			log.debug("methodName2:" + methodName);
		}
		return methodName;
	}
}
